package game.targets.entity;

import game.targets.Locator;

import com.badlogic.gdx.math.Quaternion;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.bullet.collision.btBoxShape;
import com.badlogic.gdx.physics.bullet.collision.btCollisionShape;
import com.badlogic.gdx.physics.bullet.collision.btCylinderShape;
import com.badlogic.gdx.physics.bullet.dynamics.btRigidBody;
import com.badlogic.gdx.physics.bullet.dynamics.btRigidBodyConstructionInfo;
import com.badlogic.gdx.physics.bullet.linearmath.btDefaultMotionState;
import com.badlogic.gdx.physics.bullet.linearmath.btTransform;

public class PhysicsComponent {

	protected float scale;
	protected btRigidBody body;
	protected btCollisionShape shape;
	protected btTransform transform;
	protected btDefaultMotionState myMotionState;
	protected btRigidBodyConstructionInfo rbInfo;

	public PhysicsComponent(Vector3 pos, Vector3 rotation, float mass, CollisionEntity ce, Vector3 vertices, float scale, boolean isStatic, boolean isCylinder) {
		this.scale = scale;
		// create box bodies
		if (!isCylinder) {
			//shape =new btConeShape(vertices.z, vertices.x);
			shape = new btBoxShape(vertices);
		}
		// create cylinder bodies
		else {
			shape = new btCylinderShape(vertices);
			
		}
		if (isStatic) {
			mass = 0;
		}
		Vector3 localInertia = new Vector3(pos);
		shape.calculateLocalInertia(mass, localInertia);

		transform= new btTransform();
		transform.setIdentity();
		transform.setRotation(new Quaternion(rotation,90));
		transform.setOrigin(pos);

		myMotionState = new btDefaultMotionState(transform.inverse().inv());
		rbInfo = new btRigidBodyConstructionInfo(mass, myMotionState, shape, localInertia);
		body = new btRigidBody(rbInfo);
		Locator.getWorld().addRigidBody(body);
		body.userData=ce;
	}

	public PhysicsComponent(Vector3 pos, CollisionEntity ce, Vector3 vertices, float scale, boolean isStatic, boolean isCylinder) {
		this(pos, new Vector3(0, 0, 0), 1, ce, vertices, scale, isStatic, isCylinder);
	}

	public void dispose() {
		shape.dispose();
		myMotionState.dispose();
		body.dispose();
		myMotionState=null;
		shape=null;
		body=null;
	}

	public float getScale() {
		return scale;
	}

	public void setScale(float scale) {
		this.scale = scale;
		body.getWorldTransform().scl(scale);
	}

	public btRigidBody getBody() {
		return body;
	}

	public void setBody(btRigidBody body) {
		this.body = body;
	}

	public btCollisionShape getShape() {
		return shape;
	}

	public void setShape(btCollisionShape shape) {
		this.shape = shape;
	}

}
